package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.device;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.DeviceParamerMapper;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.EntityTypeParamerMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.*;
import com.cqhilink.iot.jobstandardmanagement.facade.service.device.JsmDeviceParamerService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.dic.JsmDicService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by huangxueqian on 2017/4/28/028.
 * 设备参数实现服务
 */
public class JsmDeviceParamerServiceImpl implements JsmDeviceParamerService {
    @Autowired
    private DeviceParamerMapper deviceParamerMapper;
    @Autowired
    private EntityTypeParamerMapper eTParamerMapper;
    @Autowired
    private JsmDicService jsmDicService;

    /**
     * @param deviceId 设备id
     * @param deviceParamer 设备参数值新增
     * @param employee 操作人
     */
    @Override
    public int insertDeviceParamer(String deviceId, DeviceParamer deviceParamer, EmployeeVo employee) throws Exception {
        int resInt = 0;
        if (StringUtils.isEmpty(deviceParamer.getDeviceParamId())) {
            deviceParamer.setDeviceParamId(UniqueUtil.uuid16());
            // 查询当前设备的设备类型参数的Id,查询其他信息
            if (StringUtils.isNotEmpty(deviceParamer.getEntityTypeParamId())) {
                EntityTypeParamer entityTypeParamer = eTParamerMapper.selectByPrimaryKey(deviceParamer.getEntityTypeParamId());
                // 设置参数名称
                if (StringUtils.isNotEmpty(entityTypeParamer.getParamDataName())) {
                    deviceParamer.setParamDataName(entityTypeParamer.getParamDataName());
                }
                // 设置设备类型ID
                if (StringUtils.isNotEmpty(entityTypeParamer.getEntityTypeId())) {
                    deviceParamer.setEntityTypeId(entityTypeParamer.getEntityTypeId());
                }
                // 设置特性类型
                if (entityTypeParamer.getDataType() != null) {
                    deviceParamer.setDataType(entityTypeParamer.getDataType());
                }
                // 设置特性条件
                if (entityTypeParamer.getDataControl() != null) {
                    deviceParamer.setDataControl(entityTypeParamer.getDataControl());
                }
                // 设置特性字符长度控制
                if (entityTypeParamer.getDataLength() != null) {
                    deviceParamer.setDataLength(entityTypeParamer.getDataLength());
                }
                // 设置特性描述
                if (entityTypeParamer.getDataPrecision() != null) {
                    deviceParamer.setDataPrecision(entityTypeParamer.getDataPrecision());
                }
                // 设置设置数据来源
                if (entityTypeParamer.getDataSourceFrom() != null) {
                    deviceParamer.setDataSourceFrom(entityTypeParamer.getDataSourceFrom());
                }
                // 设置设置数据来源名称
                if (entityTypeParamer.getDataSourceFromName() != null) {
                    deviceParamer.setDataSourceFromName(entityTypeParamer.getDataSourceFromName());
                }
                // 自定义值， 不需要
                /*if(entityTypeParamer.getDataValue() != null){
                    deviceParamer.setDataValue(entityTypeParamer.getDataValue());
                }*/
                if (entityTypeParamer.getDataText() != null) {
                    deviceParamer.setDataText(entityTypeParamer.getDataText());
                }
                // 设置条件
                if (entityTypeParamer.getDataWhere() != null) {
                    deviceParamer.setDataWhere(entityTypeParamer.getDataWhere());
                }
                // 设置条件
                if (entityTypeParamer.getDataIsNull() != null) {
                    deviceParamer.setDataIsNull(entityTypeParamer.getDataIsNull());
                }
                // 设置条件
                if (entityTypeParamer.getDataSearch() != null) {
                    deviceParamer.setDataSearch(entityTypeParamer.getDataSearch());
                }
                // 设置条件
                if (entityTypeParamer.getDataAdd() != null) {
                    deviceParamer.setDataAdd(entityTypeParamer.getDataAdd());
                }
                // 设置条件
                if (entityTypeParamer.getDataVisible() != null) {
                    deviceParamer.setDataVisible(entityTypeParamer.getDataVisible());
                }
                // 设置条件
                if (entityTypeParamer.getDataPrimary() != null) {
                    deviceParamer.setDataPrimary(entityTypeParamer.getDataPrimary());
                }
                // 设置条件
                if (entityTypeParamer.getDataFlag() != null) {
                    deviceParamer.setDataFlag(entityTypeParamer.getDataFlag());
                }
                // 设置条件
                if (entityTypeParamer.getDataFlag() != null) {
                    deviceParamer.setDataFlag(entityTypeParamer.getDataFlag());
                }
                // 设置排序序号
                if (entityTypeParamer.getDataOrder() != null) {
                    deviceParamer.setDataOrder(entityTypeParamer.getDataOrder());
                }
            }
            deviceParamer.setDeviceId(deviceId);
            deviceParamer.setStatus(new BigDecimal("0"));
            deviceParamer.setCreateTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            deviceParamer.setCreator(employee.getEmployeeId());
            deviceParamer.setModifyer(employee.getEmployeeId());
            deviceParamer.setModifyTime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
            resInt = resInt + deviceParamerMapper.insertSelective(deviceParamer);
        } else {
            deviceParamer.setDeviceId(deviceId);
            resInt = resInt + deviceParamerMapper.updateByPrimaryKeySelective(deviceParamer);
        }
        return resInt;
    }

    /**
     * @param deviceParamer 设备分类参数值 查询
     * @return 返回分类特性
     */
    @Override
    public List<DeviceParamer> getDeviceParamers(DeviceParamer deviceParamer) {
        List<DeviceParamer> resList = new ArrayList<>();
        List<DeviceParamer> deviceParamers = deviceParamerMapper.getDeviceParamers(deviceParamer);
        if (resList != null && deviceParamers.size() > 0) {
            for (DeviceParamer dp : deviceParamers) {
                if (StringUtils.isNotEmpty(dp.getDataSourceFrom()) || StringUtils.isNotEmpty(dp.getDataSourceFromName())) {
                    Dic dic = new Dic();
                    if (StringUtils.isNotEmpty(dp.getDataSourceFrom())) {
                        dic.setDicTableName(dp.getDataSourceFrom());
                    }
                    if (StringUtils.isNotEmpty(dp.getDataSourceFromName())) {
                        dic.setDicType(dp.getDataSourceFromName());
                    }
                    try {
                        List<Dic> dicList = jsmDicService.getDicsByType(dic);
                        if (dicList != null && dicList.size() > 0) {
                            dp.setDicList(dicList);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                resList.add(dp);
            }
        }
        return resList;
    }

    /**
     * @param deviceParamerList 设备插入
     */
    @Override
    public int insertDeviceParamers(String deviceId, List<DeviceParamer> deviceParamerList, EmployeeVo employeeVo) {
        int resInt = 0;
        if (deviceParamerList != null && deviceParamerList.size() > 0) {
            for (DeviceParamer deviceParamer : deviceParamerList) {
                try {
                    resInt = resInt + insertDeviceParamer(deviceId, deviceParamer, employeeVo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return resInt;
    }

    /**
     * @param deviceId 设备id
     * @param deviceParamerList 设备分类特性
     * @param employeeVo 操作人
     */
    @Override
    public int modifyDeviceParamers(String deviceId, List<DeviceParamer> deviceParamerList, EmployeeVo employeeVo) {
        int resInt = 0;
        if (deviceParamerList != null && deviceParamerList.size() > 0) {
            for (DeviceParamer deviceParamer : deviceParamerList) {
                deviceParamer.setDeviceId(deviceId);
                resInt = resInt + deviceParamerMapper.insertSelective(deviceParamer);
            }
        }
        return resInt;
    }

    /**
     * @param deviceVO
     * @return
     */
    @Override
    public int cloneEntityTypeParamer(DeviceVO deviceVO) {
        return 0;
    }

    /**
     * @param device 分类参数
     * @param employeeVo 操作人
     * @return
     */
    @Override
    public int delDeviceParamers(Device device, EmployeeVo employeeVo) {
        return deviceParamerMapper.delDeviceParamers(device);
    }
}
